// pages/list/list.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    touchx: "",
    touchy: ""
  },

  /* 滑动事件 */
  touchStart(e) {
    console.log(e)
    var that = this;
    that.setData({
      touchx: e.changedTouches[0].clientX,
      touchy: e.changedTouches[0].clientY
    })
  },
  touchEnd(e) {
    // console.log(e)
    var that = this;
    let x = e.changedTouches[0].clientX;
    let y = e.changedTouches[0].clientY;
    // console.log(x,y);
    let turn = "";
    // console.log(y - that.data.touchy);
    // console.log(x - that.data.touchx);
    if (y - that.data.touchy > 10 && Math.abs(x - that.data.touchx) < 50) { //下滑
      turn = "down";
    } else if (y - that.data.touchy < -10 && Math.abs(x - that.data.touchx) < 50) { //上滑
      turn = "up";
    }
    //根据方向进行操作
    if (turn == 'down') {
      //下滑触发操作
      this.setData({
        addClass: "Block"
      })
    } else if (turn == 'up') {
      this.setData({
        addClass: "None"
      })
    }
  },

  // 添加计划
  addplan() {
    wx.navigateTo({
      url: '../todo/todo',
    })
  },

  /**
   * 日期转换
   */
  formatTime: function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    this.setData({
      year: year,
      month: (month > 10 ? month : 0 + month),
      day: (day > 10 ? day : 0 + day)
    })
  },

  /**
   * 星期
   */
  weekday: function () {
    var week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    var today = new Date().getDay();
    // console.log("today:" + today);
    switch (today) {
      case 0:
        this.setData({
          weekday: week[0]
        })
        break;
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
        this.setData({
          weekday: week[today]
        })
        break;
    }
  },

  // 初始化页面数据
  initpage() {
    wx.request({
      url: 'http://localhost:3000/todo/getdata',
      method: 'POST',
      data: {
        userId: app.globalData.openid
      },
      success: res => {
        console.log('网络请求成功', res);
        if (res.data.ok) {
          console.log('数据库查询成功');
          this.setData({
            planList: res.data.allPlanList,
            planLen: res.data.allPlanList.length,
            finishLen: res.data.finishLen
          })
        } else {
          console.log('数据库查询失败');
        }
      },
      fail: err => {
        console.log('网络请求失败', err);
      }
    })
  },

  // 完成次数加一
  addtimes(e) {
    console.log(e.target);
    wx.request({
      url: 'http://localhost:3000/todo/update',
      method: 'POST',
      data: {
        nowTime: this.data.year + '-' + this.data.month + '-' + this.data.day,
        planId: e.target.id
      },
      success: res => {
        console.log('网络请求成功', res);
        this.initpage();
      },
      fail: err => {
        console.log('网络请求失败', err);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.formatTime(new Date());
    this.weekday();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.initpage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})